package com.iplant.mes.controller.bcm;

import com.iplant.aps.service.po.oms.OMSOrder;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.annotation.trace.TraceAPI;
import com.iplant.base.utils.annotation.trace.TraceModuleAPI;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.orm.ORMUtils;
import com.iplant.base.utils.sql.orm.PoService;
import com.iplant.basic.controller.BasicController;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.mes.service.BCMService;
import com.iplant.mes.service.po.bcm.BCMRule;
import com.iplant.mes.service.po.bcm.BCMRuleCode;
import com.iplant.mes.service.po.sfc.SFCTaskSelf;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@TraceModuleAPI("条码编号")
@RestController
@RequestMapping("/api/BCMRuleCode")
public class BCMRuleCodeController extends BasicController<BCMRuleCode> {

    protected PoService<BCMRule> mRulePoService;

    @Autowired
    BCMService mBCMService;

    public BCMRuleCodeController() {
        super(BCMRuleCode.class);

        mRulePoService = ORMUtils.GetPoService(BCMRule.class);
    }

    /**
     * 新增或更新
     */
    @TraceAPI(value = "使用规则创建条码")
    @PostMapping("/CreateByRule")
    public Object CreateByRule(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult;
        try {
            BMSEmployee wLoginUser = GetSession(request);

            if (wParam == null)
                wParam = new HashMap<>();

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }
            OMSOrder wOMSOrder = null;
            if (wParam.containsKey("order")) {
                wOMSOrder = CloneTool.Clone(wParam.get("order"), OMSOrder.class);
            }

            BCMRule wBCMRule = CloneTool.Clone(wParam.get("data"), BCMRule.class);

            int wNumber = wParam.containsKey("Number") ? (int) wParam.get("Number") : 1;

            ServiceResult<List<BCMRuleCode>> wServiceResult = mBCMService.BCM_CreateRuleCodeByRule(wLoginUser,
                    wOMSOrder, wBCMRule, wNumber, true);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.getResult(), null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            log.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 新增或更新
     */
    @TraceAPI(value = "使用类型创建条码")
    @PostMapping("/CreateByType")
    public Object CreateByType(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult;
        try {
            BMSEmployee wLoginUser = GetSession(request);

            if (wParam == null)
                wParam = new HashMap<>();

            if (!wParam.containsKey("ComponentType") || !wParam.containsKey("ProductTypeID")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }
            OMSOrder wOMSOrder = null;
            if (wParam.containsKey("order")) {
                wOMSOrder = CloneTool.Clone(wParam.get("order"), OMSOrder.class);
            }
            int wType = StringUtils.parseInt(wParam.get("ComponentType"));
            int wProductTypeID = StringUtils.parseInt(wParam.get("ProductTypeID"));

            int wNumber = wParam.containsKey("Number") ? (int) wParam.get("Number") : 1;

            ServiceResult<List<BCMRuleCode>> wServiceResult = mBCMService.BCM_CreateRuleCodeByType(wLoginUser,
                    wOMSOrder, wType, wProductTypeID, wNumber);


            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.getResult(), null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            log.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 新增或更新
     */
    @TraceAPI(value = "使用派工任务创建条码")
    @PostMapping("/CreateByTask")
    public Object CreateByTask(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult;
        try {
            BMSEmployee wLoginUser = GetSession(request);

            if (wParam == null)
                wParam = new HashMap<>();

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }
            SFCTaskSelf wSFCTaskSelf = CloneTool.Clone(wParam.get("data"), SFCTaskSelf.class);


            int wNumber = wParam.containsKey("Number") ? (int) wParam.get("Number") : 1;

            ServiceResult<List<BCMRuleCode>> wServiceResult = mBCMService.BCM_CreateRuleCodeByTask(wLoginUser,
                    wSFCTaskSelf, wNumber);


            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.getResult(), null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            log.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

}
